<template>
  <el-card>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户信息管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row :gutter="10">
      <el-col :span="8">
        <el-input v-model="uname" placeholder="请输入用户名称查询" clearable @change="listPage()"></el-input>
      </el-col>
      <el-col :span="16">
        <el-button icon="el-icon-plus" type="success" @click="openAddDialog">添加</el-button>
      </el-col>
    </el-row>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="uname" label="用户名称" width="180">
      </el-table-column>
      <el-table-column prop="password" label="密码" width="180">
      </el-table-column>
      <el-table-column prop="images" label="头像">
        <template slot-scope="scope">
          <img :src="scope.row.images" style="width: 40px;height: 40px;">
        </template>
      </el-table-column>

      <el-table-column prop="stats" label="状态">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.stats"
          @change="changeStats(scope.row)"
           :active-value="0" :inactive-value="1" active-color="#13ce66" inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="点击修改" placement="bottom">
            <el-button type="warning" @click="openEditDialog(scope.row)" icon="el-icon-edit" circle></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="点击删除" placement="bottom">
            <el-button @click="remove(scope.row)" type="danger" icon="el-icon-delete" circle></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pager.page"
      :page-sizes="[5, 10, 15, 20]" :page-size="pager.size" layout="total, sizes, prev, pager, next, jumper"
      :total="pager.total">
    </el-pagination>
    <el-dialog title="用户添加" :show-close="false" :close-on-click-modal="false" :visible.sync="addDialogFlag">
      <el-form :model="users" :rules="rules" ref="users">
        <el-form-item label="用户名称" :label-width="formLabelWidth" prop="uname">
          <el-input v-model="users.uname" placeholder="请输入用户名称"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" :label-width="formLabelWidth" prop="password">
          <el-input v-model="users.password" placeholder="请输入用户密码"></el-input>
        </el-form-item>
        <el-form-item label="头像链接" :label-width="formLabelWidth" prop="uname">
          <el-input v-model="users.images" placeholder="请输入头像链接"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFlag=!addDialogFlag">取 消</el-button>
        <el-button type="primary" @click="save()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="用户修改" :show-close="false" :close-on-click-modal="false" :visible.sync="editDialogFlag">
      <el-form :model="users" :rules="rules" ref="users">
        <el-form-item label="用户名称" :label-width="formLabelWidth" prop="uname">
          <el-input v-model="users.uname" placeholder="请输入用户名称"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" :label-width="formLabelWidth" prop="password">
          <el-input v-model="users.password" placeholder="请输入用户密码"></el-input>
        </el-form-item>
        <el-form-item label="头像链接" :label-width="formLabelWidth" prop="uname">
          <el-input v-model="users.images" placeholder="请输入头像链接"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFlag=!editDialogFlag">取 消</el-button>
        <el-button type="primary" @click="update()">确 定</el-button>
      </div>
    </el-dialog>

  </el-card>

</template>

<script>
  export default {
    data() {
      return {
        uname:'',
        rules: {
          uname: [{
            required: true,
            message: '请输入用户名称',
            trigger: 'blur'
          }],
          paswword: [{
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }],
          images: [{
            required: true,
            message: '请输入头像链接地址',
            trigger: 'blur'
          }],

        },
        users: {
          uid: 0,
          uname: '',
          images:''
        },
        addDialogFlag: false,
        editDialogFlag: false,
        tableData: [],
        pager: {
          page: 1,
          size: 5,
          total: 0
        }
      }
    },
    methods: {
      changeStats(row){
        this.users=row;
        this.$http.post('http://127.0.0.1/users/changeStats', this.users)
          .then(res => {
            if (res.data.code == 200) {
              this.$message({
                message: res.data.message,
                type: 'success'
              })
              this.listPage()
            }
          }).catch(error => {
            this.$message.error('网络异常')
          })
      },
      openAddDialog() {
        this.addDialogFlag = !this.addDialogFlag;
      },
      openEditDialog(row) {
        this.users = row;
        this.editDialogFlag = !this.editDialogFlag;
      },
      save() {
        // let params = new URLSearchParams();
        // params.append("catename",this.category.catename)
        this.$refs['users'].validate(valid => {
          if (valid) {
            this.$http.post('http://127.0.0.1/users/save', this.users)
              .then(res => {
                if (res.data.code == 200) {
                  this.$message({
                    message: res.data.message,
                    type: 'success'
                  })
                  this.addDialogFlag = !this.addDialogFlag;
                  this.listPage()
                }
              }).catch(error => {
                this.$message.error('网络异常')
              })
          }
        })

      },
      update() {
        // let params = new URLSearchParams();
        // params.append("catename",this.category.catename)
        // params.append("cateid",this.category.cateid)
        this.$refs['users'].validate(valid => {
          if (valid) {
            this.$http.post('http://127.0.0.1/users/update', this.users)
              .then(res => {
                if (res.data.code == 200) {
                  this.$message({
                    message: res.data.message,
                    type: 'success'
                  })
                  this.editDialogFlag = !this.editDialogFlag;
                  this.listPage()
                }
              }).catch(error => {
                this.$message.error('网络异常')
              })
          }
        })

      },
      remove(row) {
        this.$http.post('http://127.0.0.1/users/remove?uid=' + row.uid)
          .then(res => {
            if (res.data.code == 200) {
              this.$message({
                message: res.data.message,
                type: 'success'
              })
              this.listPage()
            }
          }).catch(error => {
            this.$message.error('网络异常')
          })
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pager.size = val;
        this.listPage();
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.pager.page = val;
        this.listPage();
      },
      listPage() {
        this.$http.get('http://127.0.0.1/users/listPage', {
          params: {
            page: this.pager.page,
            size: this.pager.size,
            uname:this.uname
          }
        }).then(res => {
          console.log(res)
          this.tableData = res.data.data.data;
          this.pager.total = res.data.data.total;
        }).catch(error => {
          this.$message.error('网络异常')
        })
      }
    },
    created() {
      this.listPage();

    }
  }
</script>

<style>
  .el-breadcrumb {
    padding-bottom: 20px;
  }

  .el-pagination {
    padding-top: 10px;
  }
</style>
